package com.hsd.netty.server.strategy;

import com.hsd.netty.server.common.NettyHeader;
import com.hsd.netty.server.common.NettyMessage;
import com.hsd.netty.server.enums.MessageCode;
import com.hsd.netty.server.enums.MessageType;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;

/**
 * @author s.he
 * on 2020/6/1
 */
public class HeartBeatMessageHandler extends MessageTypeHandler {
    private static final Logger logger = LoggerFactory.getLogger(HeartBeatMessageHandler.class);

    @Override
    public NettyMessage handler(ChannelHandlerContext channelHandlerContext, NettyMessage nettyMessage) {
        NettyHeader nettyHeader = nettyMessage.getNettyHeader();
        long sessionId = nettyHeader.getSessionId();
        InetSocketAddress socketAddress = (InetSocketAddress)channelHandlerContext.channel().remoteAddress();
       logger.info("heartBeat-------------------------接收到客户端的心跳信息， ip地址为:" + socketAddress);
        String hostAddress = socketAddress.getAddress().getHostAddress();
        loginInfoMap.put(hostAddress, String.valueOf(sessionId));

        NettyMessage result = new NettyMessage();
        NettyHeader header = new NettyHeader();
        header.setSessionId(sessionId);
        header.setStatusCode(MessageCode.SUCCESS.getCode());
        header.setType(MessageType.HEART_BEAT.getCode());
        result.setNettyHeader(header);
        return result;
    }
}
